package com.shd.edanpei.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shd.edanpei.R;
import com.shd.edanpei.ui.adapter.DemandAdapter;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.base.rxbus.Subscribe;
import com.shd.edanpei.presenter.contract.DemandContract;
import com.shd.edanpei.presenter.DemandPresenter;
import com.shd.edanpei.view.RefreshLayout;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mc on 2018/10/18.
 * yang-wen-wen
 */

public class DemandActivity extends BaseActivity<DemandPresenter> implements DemandContract.DemandIView {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.iv_headline_right)
    ImageView ivRight;
    @BindView(R.id.refresh_layout)
    RefreshLayout refreshLayout;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    DemandAdapter mAdapter;

    private Bundle bundle = new Bundle();

    @Override
    protected int setLayout() {
        return R.layout.activity_demand;
    }

    @Override
    protected DemandPresenter bindPresenter() {
        return new DemandPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        RxBus.get().register(this);
        headline.setText("我需求");
        ivRight.setImageDrawable(getResources().getDrawable(R.drawable.ic_product_add));
        ivRight.setVisibility(View.VISIBLE);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new DemandAdapter(this);
        recyclerView.setAdapter(mAdapter);
        refreshLayout.setRefreshing(true);
        mPresenter.refresh();
    }

    @Override
    protected void initListener() {
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.refresh();
            }
        });
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.content:
                        bundle.clear();
                        bundle.putInt(Constant.ID, mAdapter.getItem(position).getRequirementId());
                        startActivity(NeedDetailsActivity.class, bundle);
                        break;
                    case R.id.delete:
                        mPresenter.delete(mAdapter.getItem(position).getRequirementId(), position);
                        break;
                    default:
                        break;
                }
            }
        });
    }

    @OnClick(R.id.iv_headline_back)
    void back() {
        finish();
    }

    @OnClick(R.id.iv_headline_right)
    void onRight() {
        startActivity(NewDemandActivity.class);
    }

    @Override
    public RefreshLayout getRefreshLayout() {
        return refreshLayout;
    }

    @Override
    public DemandAdapter getAdapter() {
        return mAdapter;
    }

    @Subscribe(code = RxBusCode.REFRESH_NEED_LIST)
    public void refresh() {
        mPresenter.refresh();
    }

    @Override
    protected void onDestroy() {
        RxBus.get().unRegister(this);
        super.onDestroy();
    }
}
